home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0200_Create a new file with the .wav extensio.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-11-29  |  4.0 KB  |  165 lines

  1.  
  2. This document describes the process for creating added
  3. functionality ,that many Delphi users have requested,
  4. to the TMediaPlayer. The new functionality is the ability
  5. to create a new file with the .wav format when recording.
  6. The procedure "SaveMedia" creates a record type that is
  7. passed to the MCISend command. There is an appexception
  8. that calls close media if any error occurs while attempting
  9. to open the specified file. The application consists two
  10. buttons. Button1 calls the OpenMedia and RecordMedia
  11. procedures in that order.The CloseMedia procedure is called
  12. whenever an exception is generated in this application.
  13. Button2 calls the StopMedia,SaveMedia, and CloseMedia
  14. procedures.
  15.  
  16.  
  17. unit utestrec;
  18.  
  19. interface
  20.  
  21. uses
  22.   Windows, Messages, SysUtils, Classes, Graphics, Controls,
  23.  Forms, Dialogs,MPlayer,MMSystem,StdCtrls;
  24.  
  25. type
  26.   TForm1 = class(TForm)
  27.     Button1: TButton;
  28.     Button2: TButton;
  29.     procedure Button1Click(Sender: TObject);
  30.     procedure Button2Click(Sender: TObject);
  31.     procedure FormCreate(Sender: TObject);
  32.     procedure AppException(Sender: TObject; E: Exception);
  33.   private
  34.     FDeviceID: Word;
  35.     { Private declarations }
  36.   public
  37.     procedure OpenMedia;
  38.     procedure RecordMedia;
  39.     procedure StopMedia;
  40.     procedure SaveMedia;
  41.     procedure CloseMedia;
  42.   end;
  43.  
  44. var
  45.   Form1: TForm1;
  46.  
  47. implementation
  48.  
  49. {$R *.DFM}
  50.  
  51. var
  52.   MyError,Flags: Longint;
  53.  
  54.   procedure TForm1.OpenMedia;
  55.   var
  56.     MyOpenParms: TMCI_Open_Parms;
  57.     MyPChar: PChar;
  58.     TextLen: Longint;
  59.   begin
  60.     Flags:=mci_Wait or mci_Open_Element or mci_Open_Type;
  61.   with MyOpenParms do
  62.     begin
  63.       dwCallback:=Handle; // TForm1.Handle
  64.       lpstrDeviceType:=PChar('WaveAudio');
  65.       lpstrElementName:=PChar('');
  66.     end;
  67.   MyError:=mciSendCommand(0, mci_Open, Flags,
  68.       Longint(@MyOpenParms));
  69.   if MyError = 0 then
  70.     FDeviceID:=MyOpenParms.wDeviceID;
  71.   end;
  72.  
  73.   procedure TForm1.RecordMedia;
  74.   var
  75.     MyRecordParms: TMCI_Record_Parms;
  76.     TextLen: Longint;
  77.   begin
  78.     Flags:=mci_Notify;
  79.     with MyRecordParms do
  80.     begin
  81.       dwCallback:=Handle;  // TForm1.Handle
  82.       dwFrom:=0;
  83.       dwTo:=10000;
  84.     end;
  85.     MyError:=mciSendCommand(FDeviceID, mci_Record, Flags,
  86.     Longint(@MyRecordParms));
  87.   end;
  88.  
  89.   procedure TForm1.StopMedia;
  90.   var
  91.     MyGenParms: TMCI_Generic_Parms;
  92.   begin
  93.   if FDeviceID <> 0 then
  94.     begin
  95.       Flags:=mci_Wait;
  96.       MyGenParms.dwCallback:=Handle;  // TForm1.Handle
  97.       MyError:=mciSendCommand(FDeviceID, mci_Stop, Flags,
  98.       Longint(@MyGenParms));
  99.     end;
  100.   end;
  101.  
  102.   procedure TForm1.SaveMedia;
  103.     type    // not implemented by Delphi 
  104.       PMCI_Save_Parms = ^TMCI_Save_Parms;
  105.       TMCI_Save_Parms = record
  106.       dwCallback: DWord;
  107.       lpstrFileName: PAnsiChar;  // name of file to save
  108.     end;
  109.   var
  110.     MySaveParms: TMCI_Save_Parms;
  111.   begin
  112.     if FDeviceID <> 0 then
  113.     begin
  114.         // save the file...
  115.       Flags:=mci_Save_File or mci_Wait;
  116.       with MySaveParms do
  117.         begin
  118.           dwCallback:=Handle;
  119.           lpstrFileName:=PChar('c:\message.wav');
  120.         end;
  121.       MyError:=mciSendCommand(FDeviceID, mci_Save, Flags,
  122.       Longint(@MySaveParms));
  123.     end;
  124.   end;
  125.  
  126.   procedure TForm1.CloseMedia;
  127.   var
  128.     MyGenParms: TMCI_Generic_Parms;
  129.   begin
  130.     if FDeviceID <> 0 then
  131.     begin
  132.       Flags:=0;
  133.       MyGenParms.dwCallback:=Handle; // TForm1.Handle
  134.       MyError:=mciSendCommand(FDeviceID, mci_Close, Flags,
  135.       Longint(@MyGenParms));
  136.       if MyError = 0 then
  137.         FDeviceID:=0;
  138.     end;
  139.   end;
  140.  
  141.   procedure TForm1.Button1Click(Sender: TObject);
  142.   begin
  143.     OpenMedia;
  144.     RecordMedia;
  145.   end;
  146.  
  147.   procedure TForm1.Button2Click(Sender: TObject);
  148.   begin
  149.     StopMedia;
  150.     SaveMedia;
  151.     CloseMedia;
  152.   end;
  153.  
  154.   procedure TForm1.FormCreate(Sender: TObject);
  155.   begin
  156.     Application.OnException := AppException;
  157.   end;
  158.  
  159.   procedure TForm1.AppException(Sender: TObject; E: Exception);
  160.   begin
  161.     CloseMedia;
  162.   end;
  163.  
  164. end.
  165.